﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

public partial class Management_Admin_AdminList : PageBase2
{
	private RoleManager m_RoleManager	= new RoleManager();
	private UserManager m_UserManager	= new UserManager();

	protected void Page_Load(object sender, EventArgs e)
	{
        if (this.SignInUser1 == null)
        {
            Session.Remove("SignInUser1");
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
            return;
        }
        if (!IsPostBack)
        {
            Cosmetic.DataEntity.User theUser = this.SignInUser1;
            if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
        }

		if (!IsPostBack)
		{
			this.BindRole();
			this.ddlRole_SelectedIndexChanged(null, null);
		}
	}

	private void BindRole()
	{
		foreach (string theRoleName in Enum.GetNames(typeof(Cosmetic.Logic.RoleManager.ERole)))
		{
			this.ddlRole.Items.Add(new ListItem(theRoleName, theRoleName));
		}
	}


	protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
	{
		this.BindAdmin();
	}


	private void BindAdmin()
	{
		Cosmetic.Logic.RoleManager.ERole theRole	= (RoleManager.ERole)Enum.Parse(typeof(RoleManager.ERole), this.ddlRole.SelectedValue);
		List<UserRole> theAdminList		= this.m_RoleManager.GetUserRoleByRole(theRole);

		this.lvAdmin.DataSource			= theAdminList;
		this.lvAdmin.DataBind();
	}

	protected void lvAdmin_ItemCommand(object sender, ListViewCommandEventArgs e)
	{
		if (e.Item.ItemType == ListViewItemType.DataItem)
		{
			UserRole theUserRole	= this.m_RoleManager.GetUserRoleById(new Guid(e.CommandArgument.ToString()));
			if (e.CommandName.Equals("OnEdit"))
			{
				this.txtAdmin.Enabled		= false;
				this.btnInsert.Visible		= false;
				this.btnUpdate.Visible		= true;
				this.btnCancel.Visible		= true;
				this.txtAdmin.Text			= theUserRole.User.Email;
				this.hfUserRoleId.Value		= theUserRole.UserRoleId.ToString();
			}
			else if (e.CommandName.Equals("OnDelete"))
			{
				this.m_RoleManager.Delete(theUserRole);
			}
		}
	}

	protected void btnInsert_Click(object sender, EventArgs e)
	{
		User theNewAdmin	= this.m_UserManager.GetUserByEmail(this.txtAdmin.Text.Trim());

		if (null == theNewAdmin)
		{
			Cosmetic.WebGeneric.WebMessageBox("该用户不存在!", this.Page);
			return;
		}

		UserRole theUserRole		= new UserRole();
		theUserRole.CreateTime		= DateTime.Now;
        theUserRole.RoleCategoryId = this.ddlUserRoleCategory.SelectedCategory.CategoryId;
		theUserRole.UserId			= theNewAdmin.UserId;
		theUserRole.UserRoleId		= Guid.NewGuid();
		this.m_RoleManager.Insert(theUserRole);
		Cosmetic.WebGeneric.WebMessageBox("权限赋予成功!", this.Page);

		this.txtAdmin.Text			= String.Empty;
		this.BindAdmin();
	}

	protected void btnUpdate_Click(object sender, EventArgs e)
	{
		UserRole theUserRole		= this.m_RoleManager.GetUserRoleById(new Guid(this.hfUserRoleId.Value));
        theUserRole.RoleCategoryId = this.ddlUserRoleCategory.SelectedCategory.CategoryId;
		this.m_RoleManager.Update();

		Cosmetic.WebGeneric.WebMessageBox("权限修改成功!", this.Page);

		this.txtAdmin.Enabled		= true;
		this.btnInsert.Visible		= true;
		this.btnUpdate.Visible		= false;
		this.btnCancel.Visible		= false;
		this.hfUserRoleId.Value		= String.Empty;
		this.txtAdmin.Text			= String.Empty;

		this.BindAdmin();
	}

	protected void btnCancel_Click(object sender, EventArgs e)
	{
		this.txtAdmin.Enabled		= true;
		this.btnInsert.Visible		= true;
		this.btnUpdate.Visible		= false;
		this.btnCancel.Visible		= false;
		this.hfUserRoleId.Value		= String.Empty;
		this.txtAdmin.Text			= String.Empty;
	}
}
